import requests
import json
from math import radians, cos, sin, asin, sqrt

def geoLoc(address='北京'):
#  （）是自己申请的key
    APIkey = "（）"
    url=f"https://api.map.baidu.com/geocoding/v3/?&address={address}&output=json&ak={APIkey}"
    res = requests.get(url)
    jsonData = json.loads(res.text,strict=False)
    lng = float(jsonData['result']['location']['lng'])
    lat = float(jsonData['result']['location']['lat'])
    return (lng,lat)
#
def distance(lng1,lat1,lng2,lat2):
    (lng1, lat1, lng2, lat2) = map(radians, [lng1, lat1, lng2, lat2])
    # haversine公式
    dlng = lng2 - lng1
    dlat = lat2 - lat1
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlng/2)**2
    c = 2 * asin(sqrt(a))
    r = 6371 # 地球平均半径，单位为公里
    return int(c * r)
##
if __name__ == '__main__':
    print(distance("湖南-娄底","北京-东城区"))